#include "EdlParser.h"
#include "Entity.h"
#include <QDomDocument>
#include <QFile>
#include <QString>
#include <QDomNodeList>
#include <QVector>
#include <QStringList>
#include <stdio.h>


EdlParser::EdlParser()
{
}

bool EdlParser::parseDocument(QString filename) {
    doc = QDomDocument();
    QFile file(filename);
    if ( !file.open( QIODevice::ReadOnly ) )
    {
        return false;
    }
    if ( !doc.setContent( &file ) )
    {
      file.close();
      return false;
    }
    file.close();
    return true;
}

QVector<Entity> *EdlParser::getObjects() {

    QDomNodeList obstacleElements = doc.elementsByTagName(QString("obstacle"));
    QVector<Entity> *vector = new QVector<Entity>();

    for (int i = 0; i < obstacleElements.count(); ++i)
    {
            int id = obstacleElements.at(i).attributes().namedItem("id").toAttr().value().toInt();
            int movable = obstacleElements.at(i).attributes().namedItem("movable").toAttr().value().toInt();
            QString scaling = obstacleElements.at(i).attributes().namedItem("scale").toAttr().value();
            QString delim=",";
            QStringList sc = scaling.split(delim);
            float scale[3];
            if (sc.size() == 3)
            {
                    scale[0] = sc.at(0).toFloat();
                    scale[1] = sc.at(1).toFloat();
                    scale[2] = sc.at(2).toFloat();
            }
            else
            {
                    continue;
            }
            QString type = obstacleElements.at(i).attributes().namedItem("type").toAttr().value();
            QString position = obstacleElements.at(i).attributes().namedItem("position").toAttr().value();
            QStringList p = position.split(delim);
            float pos[3];
            if (p.size() == 3)
            {
                    pos[0] = p.at(0).toFloat();
                    pos[1] = p.at(1).toFloat();
                    pos[2] = p.at(2).toFloat();
            }
            else
            {
                    continue;
            }
            QString direction = obstacleElements.at(i).attributes().namedItem("direction").toAttr().value();
            QStringList vect = direction.split(QRegExp(QString(",")));
            float v[3];
            if (vect.size() == 3)
            {
                    v[0] = vect.at(0).toFloat();
                    v[1] = vect.at(1).toFloat();
                    v[2] = vect.at(2).toFloat();
            }
            else
            {
                    continue;
            }
            QString color = obstacleElements.at(i).attributes().namedItem("color").toAttr().value();
            QStringList col = color.split(QRegExp(QString(",")));
            unsigned char c[3];
            if (col.size() == 3)
            {
                    c[0] = (unsigned char) col.at(0).toInt();
                    c[1] = (unsigned char) col.at(1).toInt();
                    c[2] = (unsigned char) col.at(2).toInt();
            }
            else
            {
                    continue;
            }

            Entity temp(id, type, pos, v, c, scale, movable);
            vector->append(temp);

    }
    return vector;

/*
    QDomNodeList objectsElement = doc.elementsByTagName(QString("objects"));
    static QDomNodeList list = objectsElement.at(0).childNodes();

    return list;
*/
}
